<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Mapping the HTTP library into the Application &lt;sup&gt;1&lt;/sup&gt;&frasl;&lt;sub&gt;2&lt;/sub&gt; - Boost.Outcome documentation</title>
<link rel="stylesheet" href="../../../css/boost.css" type="text/css">
<meta name="generator" content="Hugo 0.52 with Boostdoc theme">
<meta name="viewport" content="width=device-width,initial-scale=1.0"/>

<link rel="icon" href="../../../images/favicon.ico" type="image/ico"/>
<body><div class="spirit-nav">
<a accesskey="p" href="../../../tutorial/advanced/interop/app.html"><img src="../../../images/prev.png" alt="Prev"></a>
    <a accesskey="u" href="../../../tutorial/advanced/interop.html"><img src="../../../images/up.png" alt="Up"></a>
    <a accesskey="h" href="../../../index.html"><img src="../../../images/home.png" alt="Home"></a><a accesskey="n" href="../../../tutorial/advanced/interop/app-map-httplib2.html"><img src="../../../images/next.png" alt="Next"></a></div><div id="content">
  <div class="titlepage"><div><div><h1 style="clear: both">Mapping the HTTP library into the Application <code>1/2</code></h1></div></div></div>
  <p>Firstly, remember that we are the application writer who has the problem of
integrating three third party libraries into our application&rsquo;s Outcome-based
failure handling mechanism. We cannot modify those third party library
sources; we must be <em>non-intrusive</em>.</p>

<p>We start by dealing with the HTTP library. We will integrate this
into our application by wrapping up <code>httplib::failure</code> into a custom
STL exception type. We then type erase it into an <code>exception_ptr</code>
instance. Please note that this code is exclusively defined in the <code>app</code> namespace:</p>

<div class="code-snippet"><div class="highlight"><pre class="chroma"><code class="language-c++" data-lang="c++"><span class="k">namespace</span> <span class="n">app</span>
<span class="p">{</span>
  <span class="c1">// Specialise an exception type for httplib errors
</span><span class="c1"></span>  <span class="k">struct</span> <span class="nl">httplib_error</span> <span class="p">:</span> <span class="n">std</span><span class="o">::</span><span class="n">runtime_error</span>
  <span class="p">{</span>
    <span class="c1">// passthrough
</span><span class="c1"></span>    <span class="k">using</span> <span class="n">std</span><span class="o">::</span><span class="n">runtime_error</span><span class="o">::</span><span class="n">runtime_error</span><span class="p">;</span>
    <span class="n">httplib_error</span><span class="p">(</span><span class="n">httplib</span><span class="o">::</span><span class="n">failure</span> <span class="n">_failure</span><span class="p">,</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="n">msg</span><span class="p">)</span>
        <span class="o">:</span> <span class="n">std</span><span class="o">::</span><span class="n">runtime_error</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">move</span><span class="p">(</span><span class="n">msg</span><span class="p">))</span>
        <span class="p">,</span> <span class="n">failure</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">move</span><span class="p">(</span><span class="n">_failure</span><span class="p">))</span>
    <span class="p">{</span>
    <span class="p">}</span>

    <span class="c1">// the original failure
</span><span class="c1"></span>    <span class="n">httplib</span><span class="o">::</span><span class="n">failure</span> <span class="n">failure</span><span class="p">;</span>
  <span class="p">};</span>

  <span class="c1">// Type erase httplib::result&lt;U&gt; into a httplib_error exception ptr
</span><span class="c1"></span>  <span class="k">template</span> <span class="o">&lt;</span><span class="k">class</span><span class="err"> </span><span class="nc">U</span><span class="o">&gt;</span>  <span class="c1">//
</span><span class="c1"></span>  <span class="kr">inline</span> <span class="n">std</span><span class="o">::</span><span class="n">exception_ptr</span> <span class="n">make_httplib_exception</span><span class="p">(</span><span class="k">const</span> <span class="n">httplib</span><span class="o">::</span><span class="n">result</span><span class="o">&lt;</span><span class="n">U</span><span class="o">&gt;</span> <span class="o">&amp;</span><span class="n">src</span><span class="p">)</span>
  <span class="p">{</span>
    <span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="n">str</span><span class="p">(</span><span class="s">&#34;httplib failed with error &#34;</span><span class="p">);</span>
    <span class="k">switch</span><span class="p">(</span><span class="n">src</span><span class="p">.</span><span class="n">error</span><span class="p">().</span><span class="n">status</span><span class="p">)</span>
    <span class="p">{</span>
    <span class="k">case</span> <span class="n">httplib</span><span class="o">::</span><span class="n">status_code</span><span class="o">::</span><span class="nl">success</span><span class="p">:</span>
      <span class="n">str</span><span class="p">.</span><span class="n">append</span><span class="p">(</span><span class="s">&#34;success&#34;</span><span class="p">);</span>
      <span class="k">break</span><span class="p">;</span>
    <span class="k">case</span> <span class="n">httplib</span><span class="o">::</span><span class="n">status_code</span><span class="o">::</span><span class="nl">bad_request</span><span class="p">:</span>
      <span class="n">str</span><span class="p">.</span><span class="n">append</span><span class="p">(</span><span class="s">&#34;bad request&#34;</span><span class="p">);</span>
      <span class="k">break</span><span class="p">;</span>
    <span class="k">case</span> <span class="n">httplib</span><span class="o">::</span><span class="n">status_code</span><span class="o">::</span><span class="nl">access_denied</span><span class="p">:</span>
      <span class="n">str</span><span class="p">.</span><span class="n">append</span><span class="p">(</span><span class="s">&#34;access denied&#34;</span><span class="p">);</span>
      <span class="k">break</span><span class="p">;</span>
    <span class="k">case</span> <span class="n">httplib</span><span class="o">::</span><span class="n">status_code</span><span class="o">::</span><span class="nl">logon_failed</span><span class="p">:</span>
      <span class="n">str</span><span class="p">.</span><span class="n">append</span><span class="p">(</span><span class="s">&#34;logon failed&#34;</span><span class="p">);</span>
      <span class="k">break</span><span class="p">;</span>
    <span class="k">case</span> <span class="n">httplib</span><span class="o">::</span><span class="n">status_code</span><span class="o">::</span><span class="nl">forbidden</span><span class="p">:</span>
      <span class="n">str</span><span class="p">.</span><span class="n">append</span><span class="p">(</span><span class="s">&#34;forbidden&#34;</span><span class="p">);</span>
      <span class="k">break</span><span class="p">;</span>
    <span class="k">case</span> <span class="n">httplib</span><span class="o">::</span><span class="n">status_code</span><span class="o">::</span><span class="nl">not_found</span><span class="p">:</span>
      <span class="n">str</span><span class="p">.</span><span class="n">append</span><span class="p">(</span><span class="s">&#34;not found&#34;</span><span class="p">);</span>
      <span class="k">break</span><span class="p">;</span>
    <span class="k">case</span> <span class="n">httplib</span><span class="o">::</span><span class="n">status_code</span><span class="o">::</span><span class="nl">internal_error</span><span class="p">:</span>
      <span class="n">str</span><span class="p">.</span><span class="n">append</span><span class="p">(</span><span class="s">&#34;internal error&#34;</span><span class="p">);</span>
      <span class="k">break</span><span class="p">;</span>
    <span class="p">}</span>
    <span class="n">str</span><span class="p">.</span><span class="n">append</span><span class="p">(</span><span class="s">&#34; [url was &#34;</span><span class="p">);</span>
    <span class="n">str</span><span class="p">.</span><span class="n">append</span><span class="p">(</span><span class="n">src</span><span class="p">.</span><span class="n">error</span><span class="p">().</span><span class="n">url</span><span class="p">);</span>
    <span class="n">str</span><span class="p">.</span><span class="n">append</span><span class="p">(</span><span class="s">&#34;]&#34;</span><span class="p">);</span>
    <span class="k">return</span> <span class="n">std</span><span class="o">::</span><span class="n">make_exception_ptr</span><span class="p">(</span><span class="n">httplib_error</span><span class="p">(</span><span class="n">src</span><span class="p">.</span><span class="n">error</span><span class="p">(),</span> <span class="n">std</span><span class="o">::</span><span class="n">move</span><span class="p">(</span><span class="n">str</span><span class="p">)));</span>
  <span class="p">}</span>
<span class="p">}</span>  <span class="c1">// namespace app
</span><span class="c1"></span></code></pre></div><a href="https://github.com/boostorg/outcome/tree/master/doc/src/snippets/finale.cpp#L202" class="code-snippet-url" target="_blank">View this code on Github</a></div>


<p>Most of the complexity in this code fragment is driven by the need to create
some sort of descriptive string for <code>std::runtime_error</code>
so its <code>.what()</code> returns a useful summary of the original failure. This
is the main purpose of the <code>app::make_httplib_exception()</code> function.</p>

<p>(Note that if you have Reflection in your C++ compiler, it may be possible to script
the conversion of enum values to string representations)</p>

<p>The only real thing to note about <code>app::httplib_error</code> is that it squirrels away
the original <code>httplib::failure</code> in case that is ever needed.</p>


        </div><p><small>Last revised: February 11, 2019 at 13:38:04 UTC</small></p>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../../../tutorial/advanced/interop/app.html"><img src="../../../images/prev.png" alt="Prev"></a>
    <a accesskey="u" href="../../../tutorial/advanced/interop.html"><img src="../../../images/up.png" alt="Up"></a>
    <a accesskey="h" href="../../../index.html"><img src="../../../images/home.png" alt="Home"></a><a accesskey="n" href="../../../tutorial/advanced/interop/app-map-httplib2.html"><img src="../../../images/next.png" alt="Next"></a></div></body>
</html>
